/**
 * Copyright (c) 2007,中企动力华南研发中心<br>
 * All rights reserved.<br>
 */
package com.ce.pms.custservice.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ce.pms.base.web.PmsBaseAction;
import com.ce.pms.common.model.SysrefcodeDO;
import com.ce.pms.common.service.IOrganizationalStructureService;
import com.ce.pms.common.vo.HmDeptVO;
import com.ce.pms.custservice.service.ISubmitTaskService;
import com.ce.pms.custservice.vo.SubmitTaskSearchVO;
import com.ce.pms.custservice.vo.SubmitTaskVO;
import com.ce.pms.exception.UILayerException;
import com.ce.pms.system.custservice.service.ICustEventNatureService;
import com.ce.pms.system.custservice.vo.CustEventNatureVO;
import com.ce.util.BlankUtil;
import com.sitechasia.webx.components.xtable.util.ExtremeUtils;
import com.sitechasia.webx.core.annotation.Out;
import com.sitechasia.webx.core.annotation.Read;
import com.sitechasia.webx.core.support.Page;

/**
 * 功能描述：<br>
 * @author 易贤建 创建日期：2008-12-10<br>
 */
public class DeptCooperationAction extends PmsBaseAction {



	/**
	 * 功能描述:初始化任务列表主页面<br>
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-12-10  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public String initCustomerTaskMain(@Read(key="customerEventId")@Out(key="customerEventId")Long customerEventId){
		initCustomerTaskStatus();
		return "taskCommonMain";
	}


	public String searchCustomerTaskMain(@Read(key="customerEventId")@Out(key="customerEventId")Long customerEventId){
		initCustomerTaskStatus();
		HmDeptVO hmDeptVO=organizationalStructureService.queryDeptInfoByUseID(this.getUserId());
		if(!BlankUtil.isBlank(hmDeptVO)){
			this.getRequest().setAttribute("deptId", hmDeptVO.getDeptId());
		}
		return "taskMain";
	}

	/**
	 * 功能描述:初始化客户任务列表页面<br>
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-12-10  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public String queryTaskByPage(@Read(key="customerEventId")@Out(key="customerEventId")Long customerEventId,
			@Read(key = "custServEventNatureId")@Out(key="custServEventNatureId")Long custServEventNatureId,
			@Read(key = "deptId")@Out(key="deptId")Long deptId,
			@Read(key = "deptName")@Out(key="deptName")String deptName,
			@Read(key = "houseNumber")@Out(key="houseNumber")String houseNumber,
			@Read(key = "statuz")@Out(key="statuz")String statuz,
			@Read(key="MenuFlat",defaultValue="true")@Out(key="MenuFlat")String MenuFlat,
			@Read(key = "ec_crd",defaultValue="10")	int pageSize,
            @Read(key = "ec_p", defaultValue="1")	int pageNo){
		this.getRequest().setAttribute("pageSize", new Integer(pageSize));

		SubmitTaskSearchVO searchVO = new SubmitTaskSearchVO();
		searchVO.setPageNo(pageNo);
		searchVO.setPageSize(pageSize);
		searchVO.setSortStr(ExtremeUtils.getOrderString(this.getRequest()));
		searchVO.setCustServEventNatureId(custServEventNatureId);
		searchVO.setCustServiceEventId(customerEventId);
		searchVO.setDeptId(deptId);
		searchVO.setDeptName(deptName);
		searchVO.setStatuz(statuz);
		searchVO.setHouseNumber(houseNumber);
		Page page=submitTaskService.querySubmitTaskByPage(searchVO);
		List<SubmitTaskVO> taskList = page.getResult();
		Long totalRows = page.getTotalCount();
		this.getRequest().setAttribute("submitTaskList", taskList);
		this.getRequest().setAttribute("totalRows", totalRows.intValue());

//		Page page = new Page();
//		List taskList =initTaskList();
//		page.setResult(taskList);
//		setAttribute("submitTaskList", taskList);
//		setAttribute("totalRows", new Integer(taskList.size()));
		return "taskList";
	}

	/**
	 * 功能描述:<br>
	 * @param <br>
	 * @return<br>
	 *
	 * Create author:xuhaidui<br>
	 * Create on:2008-12-19<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public String navigateTaskDetail(@Read(key="customerTaskId")Long customerTaskId){
	//	SubmitTaskVO submitTaskVO =new SubmitTaskVO();
		SubmitTaskVO submitTaskVO=submitTaskService.getSubmitTaskVOById(customerTaskId);
		this.getRequest().setAttribute("entryVO", submitTaskVO);

		return "taskDetail";
	}

	/**
	 * 功能描述:初始化任务提交页面<br>
	 * @param keyword 事件主键
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-12-17  <br>
	 */
	public String navigateTaskSubmit(@Read(key="keyword")@Out(key="keyword")Long keyword){
		return "taskSubmit";
	}
	/**
	 * 功能描述:提交任务,提交客服事件,部门负责人。业务要求可以多个部门、多个人，切对应的部门同对应的人相关联，<br>
	 * 所以页面以 “部门1-负责人1-负责人2，部门2-负责人1-负责人2” 这个格式传过来,然后在action中组装,传到Service
	 * @param custServiceEventId 客服事件ID
	 * @param principals 相关部门、负责人
	 * Create author:yixianjian <br>
	 * Create on:2008-12-17  <br>
	 */
	public void addTask(@Read SubmitTaskVO submitTaskVO,
						  @Read(key="principals")String principals){
		submitTaskVO.setPrincipalMap(assembledTaskMap(principals));
		submitTaskVO.setCreator(super.getUserId());
		Map<Long,String> result = submitTaskService.createEventTask(submitTaskVO);
		String errorMessage = getAddTaskErrorMessage(result);
		if(errorMessage != null)
			throw new UILayerException(errorMessage,UILayerException.FAILURE);
	}

	/**
	 * 功能描述: 任务回撤页面<br>
	 * Create author:yixianjian <br>
	 * Create on:2008-12-10  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public String navigateTaskRevoke(@Read(key="customerTaskId")@Out(key="customerTaskId")Long customerTaskId,
			@Read(key="custServiceEventId")@Out(key="custServiceEventId")Long custServiceEventId){
		return "taskRevoke";
	}

	/**
	 * 功能描述: 保存任务回撤<br>
	 * Create author:yixianjian <br>
	 * Create on:2008-12-10  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public String revokeTask(@Read SubmitTaskVO submitTaskVO){
		submitTaskVO.setModifier(this.getUserId());
		submitTaskService.cancelTask(submitTaskVO);
		this.getRequest().setAttribute("iframeName", "customerTaskList");
		return "onlyRefresh";
	}

	/**
	 * 功能描述: 任务回撤页面<br>
	 * Create author:yixianjian <br>
	 * Create on:2008-12-10  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public String navigateAccomplishRegister(@Read(key="customerTaskId")@Out(key="customerTaskId")Long customerTaskId,
			@Read(key="custServiceEventId")@Out(key="custServiceEventId")Long custServiceEventId){
		return "taskAccomplishRegister";
	}

	/**
	 * 功能描述: 保存任务完成<br>
	 * Create author:xuhaidui <br>
	 * Create on:2008-12-10  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public String addAccomplishRegisterInfo(@Read SubmitTaskVO submitTaskVO){
		submitTaskVO.setModifier(this.getUserId());
		submitTaskService.deptReply(submitTaskVO);
		this.getRequest().setAttribute("iframeName", "customerTaskList");
		return "onlyRefresh";
	}

	/**
	 * 功能描述: 导航到处理意见登记页面<br>
	 * Create author:xuhaidui <br>
	 * Create on:2008-12-10  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public String navigateProcessingOpinionRegister(@Read(key="customerTaskId")@Out(key="customerTaskId")Long customerTaskId){
		SubmitTaskVO submitTaskVO=submitTaskService.getSubmitTaskVOById(customerTaskId);
		if(!BlankUtil.isBlank(submitTaskVO)){
			this.getRequest().setAttribute("processingOpinion", submitTaskVO.getDealViews());
		}
		return "taskProcessingOpinionRegister";
	}

	/**
	 * 功能描述: 新增处理意见<br>
	 * Create author:xuhaidui <br>
	 * Create on:2008-12-10  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public String addProcessingOpinion(@Read(key="customerTaskId")Long customerTaskId,
			@Read(key="processingOpinion")String processingOpinion){
		SubmitTaskVO submitTaskVO=new SubmitTaskVO();
		submitTaskVO.setModifier(this.getUserId());
		submitTaskVO.setKeyword(customerTaskId);
		submitTaskVO.setDealViews(processingOpinion);
		submitTaskService.editSubmitTask(submitTaskVO);
		this.getRequest().setAttribute("iframeName", "customerTaskList");
		return "onlyRefresh";
	}

	/**
	 * 功能描述: 导航到反馈信息页面<br>
	 * Create author:xuhaidui <br>
	 * Create on:2008-12-10  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
    public String navigateTaskFeedback(@Read(key="customerTaskId")@Out(key="customerTaskId")Long taskId){
    	SubmitTaskVO submitTaskVO=submitTaskService.getSubmitTaskVOById(taskId);
    	if(!BlankUtil.isBlank(submitTaskVO)){
    	  this.getRequest().setAttribute("taskFeedbackInfo", submitTaskVO.getReplyViews());
    	}
    	return "taskFeedbackRegister";
    }

    /**
	 * 功能描述: 新增任务反馈信息<br>
	 * Create author:xuhaidui <br>
	 * Create on:2008-12-10  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
    public String addTaskFeedbackInfo(@Read(key="customerTaskId")Long taskId,
			@Read(key="taskFeedbackInfo")String taskFeedbackInfo){

    	SubmitTaskVO submitTaskVO=new SubmitTaskVO();
    	submitTaskVO.setModifier(this.getUserId());
		submitTaskVO.setKeyword(taskId);
		submitTaskVO.setReplyViews(taskFeedbackInfo);
		submitTaskService.editSubmitTask(submitTaskVO);
		this.getRequest().setAttribute("iframeName", "customerTaskList");
		return "onlyRefresh";
    }

	/**
	 * 功能描述: 保存催单<br>
	 * Create author:xuhaidui <br>
	 * Create on:2008-12-10  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public String hasten(@Read(key="customerTaskId")Long customerEventId){
		submitTaskService.hasten(customerEventId);
		this.getRequest().setAttribute("iframeName", "customerTaskList");
		return "onlyRefresh";
	}
	/**
	 * 功能描述:验证前台页面选中的当前事件是否允许提交<br>
	 * @param submitTaskVO
	 * Create author:yixianjian <br>
	 * Create on:2009-1-7  <br>
	 */
	public void validationAllowSubmit(@Read SubmitTaskVO submitTaskVO){
		submitTaskService.checkEventIsAllowSubmit(submitTaskVO);
	}
	/**
	 * 功能描述:获取配置在国际化文件中的错误信息<br>
	 * @param result
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2009-1-5  <br>
	 */
	private String getAddTaskErrorMessage(Map<Long,String> result){
		if(result == null || result.isEmpty()) return null;
		StringBuffer errorMessage = new StringBuffer();
		errorMessage.append(super.getLocaleMessage("custservice:task_add_error"));
		for(String error : result.values()){
			errorMessage.append(error).append(" ");
		}
		errorMessage.append(super.getLocaleMessage("custservice:task_add_error_after"));
		return errorMessage.toString();
	}

	/**
	 * 功能描述:初始化任务状态,任务列表页面查询条件有一个任务状态下拉框,需要初始化<br>
	 * Create author:yixianjian <br>
	 * Create on:2008-12-10  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	private void initCustomerTaskStatus(){
		List<SysrefcodeDO> statuList=getSysCodoList(SysrefcodeDO.DISPATCH_EVENT_STATUZ_CODE);
		this.getRequest().setAttribute("statuList", statuList);
        List<CustEventNatureVO> eventNatureList=custEventNatureService.queryAllCustEventNature();
        this.getRequest().setAttribute("eventNatureList", eventNatureList);
	}
	/**
	 * 功能描述: 拼装传入的部门字符串，以MAP形式组装<br>
	 * @param principals
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-12-17  <br>
	 */
	private Map<Long,List<String>> assembledTaskMap(String principals){
		if(BlankUtil.isBlank(principals)) return null;
		Map<Long,List<String>> result = new HashMap<Long, List<String>>();
		List<String> principal = null;

		String[] departments = principals.split(",");
		for(String depart : departments){
			String[] fuzerens = depart.split("-");
			principal = new ArrayList<String>();
			for(int i = 1; i <  fuzerens.length; i++){ //第一个数组对象是部门信息,后面的才是员工信息
				principal.add(fuzerens[i]);
			}
			result.put(Long.valueOf(fuzerens[0]), principal);
		}
		return result;
	}

	private List<SysrefcodeDO> getSysCodoList(String code){
		if(BlankUtil.isBlank(code))
			return null;
		return sysrefcodeService.querySysrefCodesByCode(code);
	}

	private ISubmitTaskService submitTaskService = null; //任务Service

	private IOrganizationalStructureService organizationalStructureService;

	private ICustEventNatureService	custEventNatureService;

	public void setSubmitTaskService(ISubmitTaskService submitTaskService) {
		this.submitTaskService = submitTaskService;
	}

	public void setOrganizationalStructureService(
			IOrganizationalStructureService organizationalStructureService) {
		this.organizationalStructureService = organizationalStructureService;
	}

	public void setCustEventNatureService(
			ICustEventNatureService custEventNatureService) {
		this.custEventNatureService = custEventNatureService;
	}

	/**
	 * 方法描述: 催单操作合法性校验方法
	 * @author 许海堆
	 * Create on:2008-12-24
	 * @param submitTaskID
	 * @return SubmitTaskDO
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public void checkHastenIsTrue(@Read(key="submitTaskid")Long submitTaskid){
		submitTaskService.checkHastenIsTrue(submitTaskid);
	}

	/**
	 * 方法描述: 任务撤销合法性校验方法
	 * @author 许海堆
	 * Create on:2008-12-24
	 * @param submitTaskID
	 * @return SubmitTaskDO
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public void checkCancelTaskIsTrue(@Read(key="submitTaskid")Long submitTaskid){
		submitTaskService.checkCancelTaskIsTrue(submitTaskid);
	}

	/**
	 * 方法描述: 任务修改合法性校验方法
	 * @author 胡煦
	 * Create on:2008-12-24
	 * @param submitTaskID
	 * @return SubmitTaskDO
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public void checkEditTaskIsTrue(@Read(key="submitTaskid")Long submitTaskid){
		submitTaskService.checkEditTaskIsTrue(submitTaskid);
	}

	/**
	 * 方法描述: 任务完工合法性校验方法
	 * @author 许海堆
	 * Create on:2008-12-24
	 * @param submitTaskID
	 * @return SubmitTaskDO
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public void checkDeptReplyIsTrue(@Read(key="submitTaskid")Long submitTaskid){
		submitTaskService.checkDeptReplyIsTrue(submitTaskid);
	}


}
